package com.dingwei.superiordiary.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.WelcomeBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.google.gson.Gson;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;

import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/6.
 */
public class WelcomeActivity extends BaseActivity {

    @Bind(R.id.start_bg)
    ImageView startBg;
    @Bind(R.id.btn_next)
    Button btnNext;
    private MyCount myCount;
    private WelcomeBean bean;


    @Override
    protected int getContentViewId() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window window = WelcomeActivity.this.getWindow();
        window.setFlags(flag, flag);

        return R.layout.activity_welcome;
    }

    @Override
    protected void initData() {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.appStartImg);
        rp.setConnectTimeout(5000);
        x.http().post(rp, new myCallback(BaseActivity.context, WelcomeBean.class,false) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                bean = new Gson().fromJson(result, WelcomeBean.class);
                Glide.with(context).load(bean.getData())
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Drawable> target, boolean b) {
                                myCount.start();
                                btnNext.setVisibility(View.VISIBLE);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable drawable, Object o, Target<Drawable> target, DataSource dataSource, boolean b) {
                                startBg.setImageDrawable(drawable);
                                myCount.start();
                                btnNext.setVisibility(View.VISIBLE);
                                return false;
                            }
                        })
                        .into(startBg);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                startBg.setImageResource(R.mipmap.start);
                myCount.start();
                btnNext.setVisibility(View.VISIBLE);
            }

        });

    }

    @Override
    protected void initView() {
        myCount = new MyCount(5000, 1000);
    }

    @Override
    protected void initListener() {

    }

    private Target<Drawable> target = new Target<Drawable>() {
        @Override
        public void onLoadStarted(Drawable placeholder) {

        }

        @Override
        public void onLoadFailed(@Nullable Drawable drawable) {

        }

        @Override
        public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {

        }


        @Override
        public void onLoadCleared(Drawable placeholder) {

        }

        @Override
        public void getSize(SizeReadyCallback cb) {

        }

        @Override
        public void removeCallback(SizeReadyCallback sizeReadyCallback) {

        }

        @Override
        public void setRequest(Request request) {

        }

        @Override
        public Request getRequest() {
            return null;
        }

        @Override
        public void onStart() {

        }

        @Override
        public void onStop() {

        }

        @Override
        public void onDestroy() {

        }
    };

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick({R.id.start_bg, R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_bg:
                break;
            case R.id.btn_next:
                intent.setClass(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                myCount.cancel();
                finish();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    /**
     * 倒计时
     */
    public class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            intent.setClass(WelcomeActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            btnNext.setText(millisUntilFinished / 1000 + " 跳过");
        }

    }
}
